<style lang='less'>
@import "./login2.less";
</style>

<template>
  <div class='login-container'>
    <div class='login-box' style="width: 20px;margin-left: 160px;">
      <h6 class='title'>云科谷核心交易系统</h6>
      <div class='form-container'>
        <Form ref='loginForm' :model='form' :rules='rules' @keydown.native.enter.prevent='handleLoginSubmit'>
          <FormItem prop='userCode' style="width: 220px;margin-left: 70px;" >
            <!-- <div class='form-item ion-person'> -->
            <Input v-model='form.userCode' placeholder='请输入用户名'>
            <span slot='prepend'>
              <Icon type="md-person" :size='16'/>
            </span>
            </Input>
            <!-- </div>  -->
          </FormItem>
          <FormItem prop='userPwd' style="width: 220px;margin-left: 70px;">
            <!-- <div class='form-item ion-key'> -->
            <Input type='password' v-model='form.userPwd' placeholder='请输入密码'>
            <span slot='prepend'>
              <Icon type="md-lock" :size='16'/>
            </span>
            </Input>
            <!-- </div> -->
          </FormItem>
          <FormItem style="width: 150px;margin-left: 110px;">
            <Button @click='handleLoginSubmit' type='primary'  long>登录</Button>
          </FormItem>
        </Form>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  data () {
    return {
      $ajax: null,
      form: {
        userCode: null,
        userPwd: null
      },
      rules: {
        userCode: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        userPwd: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleLoginSubmit () {
      var vm = this
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          var params = this.form
          vm.$ajax
            .post(
              this.$appContext.marvels_core_system + 'system/service/login',
              params
            )
            .then(re => {
              var data = re.data
              var code = data.code
              var message = data.message
              if (code !== 'success') {
                this.$Message.warning(message)
                return
              }
              sessionStorage['l-token'] = data.data.token
              sessionStorage['userCode'] = data.data.userCode
              sessionStorage['positionCode'] = data.data.positionCode
              sessionStorage['positionName'] = data.data.positionName
              sessionStorage['lastLoginIp'] = data.data.lastLoginIp
              sessionStorage['lastLoginTime'] = vm.$util.formatTime(data.data.lastLoginTime)
              sessionStorage['nodeGroupList'] = (data.data.nodeGroupList && data.data.nodeGroupList != null && data.data.nodeGroupList !== '') ? JSON.stringify(data.data.nodeGroupList) : ''
              sessionStorage['groupNode'] = (data.data.groupNode && data.data.groupNode !== '' && data.data.groupNode != null) ? JSON.stringify(data.data.groupNode) : ''
              if (data.data.groupNode && data.data.groupNode.nodeName && data.data.groupNode.groupName) {
                sessionStorage['menuText'] = data.data.groupNode.nodeName + '-' + data.data.groupNode.groupName
                sessionStorage['nodeCode'] = data.data.groupNode.nodeCode
                sessionStorage['groupCode'] = data.data.groupNode.groupCode
              } else if (data.data.groupNode && data.data.groupNode.nodeName) {
                sessionStorage['menuText'] = data.data.groupNode.nodeName
                sessionStorage['nodeCode'] = data.data.groupNode.nodeCode
              }
              this.$router.push({
                name: 'home'
              })
            })
            .catch(er => {})
        }
      })
    }
  },
  mounted () {
    this.$ajax = this.$util.ajax()
  }
}
</script>
